# -----App Manual-----#
#! python3
# coding:utf-8
# description: #获取指定讨论组的成员列表
# author: terry
# date:2020-01-07
# -------------------#

# **  微信会限制自动搜索讨论组的次数，每次获得的结果可能都不一样；

import itchat


# 登录网页版
#itchat.login()
itchat.auto_login(hotReload=True) #保留登录状态

print('#---------------------分割线---------------------------#')

#获取讨论组列表
# groups=itchat.get_chatrooms(update=True)
# total_chatrooms= len(groups)+1
# print("讨论组个数：",total_chatrooms)
# #打印每个讨论组的名称
# for c in groups:
#     print(groups.index(c)+1,c['NickName'],c['UserName'])

#region 1--使用土办法
#获取指定讨论组的成员列表
# 初次获得的讨论组内只有名称/id等信息，需要进行更新才可获得全面的数据；
""" room_wanted=itchat.search_chatrooms(name='Family')
if room_wanted is None:
    print ('group not found!')
else:
    print(type(room_wanted))
    print(len(room_wanted))
    for l in room_wanted:
        print (l)
    print ('#'*21)
    print(room_wanted[0]['UserName'])
    for x in room_wanted[0]['MemberList']:
        print (x)
"""

""" #找到指定的讨论组
room_wanted=itchat.search_chatrooms(name='辉煌依旧40401')
#更新组的信息
room_obj=itchat.update_chatroom(userName=room_wanted[0]['UserName'],detailedMember=True)
print(type(room_obj))
#打印获取到的好友信息
#print(room_obj['MemberList'])
print('群名称：',room_obj['NickName'])
print('成员数量:',room_obj['MemberCount']) 
for p in room_obj['MemberList']:
    print(p['NickName'],p['Sex'],p['RemarkName'],p['City']) """

#endregion

#region 2--#使用函数的方法-获取指定名称的讨论组
#"""

def grap_group_members(name_wanted):
    nickname=[]
    myroom=itchat.search_chatrooms(name=name_wanted) 
    if myroom is None:
        print('%s Group is not found' %(name_wanted))
        return nickname
    else:
        room_obj=itchat.update_chatroom(userName=myroom[0]['UserName'],detailedMember=True)
        nickname=[]
        for m in room_obj['MemberList']:
            #print (m['NickName'],m['Sex'],m['RemarkName'])
            nickname.append(m['NickName'])
    return  nickname

m_list=grap_group_members('F5 抗疫群')
print (m_list)
#"""
#endregion
